package view;

import controllers.CustomerController;
import controllers.ExportBill;
import controllers.ExportExcel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;


public class StartView extends JFrame{
	private ImageIcon image = null;
	JTextField 		tfUserName = new JTextField(10);
	JPasswordField 	pfPassWord = new JPasswordField(10);
	JButton			btLogin    = new JButton("Đăng nhập");
	public StartView() throws Exception{
		super("QUẢN LÍ KHÁCH SẠN");
		setMinimumSize(new Dimension(800, 400));
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
                initUI();
                if(!dao.Connector.connect()){
                    new view.ConfigForm(this,true).setVisible(true);                    
                }
               
	}
	
	private void initUI(){
		try{
			image = new ImageIcon("src/image/background.jpg");
		}catch(Exception e){}
		
		JPanel content = new JPanel(){
			@Override
			public void paintComponent(Graphics g){
				super.paintComponent(g);
				for (int i=0; i<=getWidth()/image.getIconWidth(); i++)
					for (int j=0; j<=getHeight()/image.getIconHeight(); j++)
						g.drawImage(image.getImage(), i*image.getIconWidth(), 
								j*image.getIconHeight(), null);
			}
		};
		
		content.setLayout(new GridBagLayout());
		
		JLabel lbWelcome = new JLabel("QUẢN LÍ KHÁCH SẠN");
		lbWelcome.setFont(new Font(lbWelcome.getFont().getName(), Font.PLAIN, 20));
		lbWelcome.setForeground(Color.BLUE);
		
		JLabel lbUserName = new JLabel("Tên đăng nhập");
		JLabel lbPassWord = new JLabel("Mật khẩu");
		
		btLogin.addActionListener(new LoginHandler());
		
		GridBagConstraints gbcx = new GridBagConstraints();
		
		gbcx.gridy = 0; gbcx.gridx = 0; gbcx.ipady = 50;
			content.add(lbWelcome, gbcx);
		JPanel jpContent = new JPanel(new GridBagLayout());
		jpContent.setPreferredSize(new Dimension(300, 140));
		jpContent.setBorder(BorderFactory.createCompoundBorder(
						BorderFactory.createBevelBorder(BevelBorder.RAISED),
						BorderFactory.createTitledBorder("Login")));
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(10, 20,0, 20);
		
		gbc.weightx = 0;
		gbc.gridy = 1; gbc.gridx = 0; gbc.gridwidth = 1;
			jpContent.add(lbUserName, gbc);
		gbc.gridy = 2; gbc.gridx = 0;
			jpContent.add(lbPassWord, gbc);
		
		gbc.weightx = 1.0;
		gbc.gridy = 1; gbc.gridx = 1;
			jpContent.add(tfUserName, gbc);
		gbc.gridy = 2; gbc.gridx = 1;
			jpContent.add(pfPassWord, gbc);
		gbc.gridy = 3; gbc.gridx = 1; gbc.gridwidth = 1; 
			jpContent.add(btLogin, gbc);
			
			
		gbcx.gridy = 1; gbcx.gridx = 0; gbcx.ipady = 0;
		
		content.add(jpContent, gbcx);
		add(content);
	}
	
	class LoginHandler implements ActionListener{
		public void actionPerformed(ActionEvent event){
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					new MainView().setVisible(true);
					dispose();
				}
			});
			
		}
	}
	
}
